package com.kust.smarhome.background.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket buildDocket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kust.smarhome.background.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(buildParameterInfo());


    }

    private List<Parameter> buildParameterInfo() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization")
                .defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNzY4NDAyNTFAcXEuY29tIiwiaWF0IjoxNjI2ODQwMzIxLCJleHAiOjE2Mjc0NDUxMjF9.Xo7DgMFINXSFGKteNqEf0fUIm7ahaIpKKQH5Q6n6LyC6IRsEbeaIMKaFN2D_CIR6-HuKnPHvxNwkxuEKnnkPjQ")
                .description("登录配置")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(tokenPar.build());

        return pars;
    }

    private ApiInfo buildApiInfo() {
        Contact contact = new Contact("iAuzre", "", "");
        return new ApiInfoBuilder()
                .title("向日葵webAPI文档")
                .description("web 相关api")
                .contact(contact)
                .version("1.0.0").build();
    }

}
